package com.example.studentapp.ui;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.studentapp.R;
import com.example.studentapp.adapters.MoneyAdapter;
import com.example.studentapp.beans.Money;
import com.example.studentapp.db.SQLiteDB;
import com.example.studentapp.money.AIRecordActivity;
import com.example.studentapp.money.AddMoneyActivity;
import com.example.studentapp.money.DetailRecordActivity;
import com.example.studentapp.money.SearchRecordActivity;

import java.util.ArrayList;
import java.util.List;


public class MoneyFragment extends Fragment {
    ImageView money_searchIV,money_aiIV;
    Button money_addBtn;
    TextView money_warnTV;
    ListView moneyLV;
    MoneyAdapter moneyAdapter;
    List<Money> list;
    SQLiteDB sqLiteDB;
    SQLiteDatabase sqLiteDatabase;


    @Override
    @SuppressLint("Range")
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View root = inflater.inflate(R.layout.fragment_money, container, false);
        money_searchIV = root.findViewById(R.id.money_searchIV);
        money_aiIV = root.findViewById(R.id.money_aiIV);
        money_addBtn = root.findViewById(R.id.money_addBtn);
        money_warnTV = root.findViewById(R.id.money_warnTV);
        moneyLV = root.findViewById(R.id.moneyLV);
        list = new ArrayList<>();

        // 数据的获取
        sqLiteDB = new SQLiteDB(getActivity());
        sqLiteDatabase = sqLiteDB.getWritableDatabase();
        String sql = "select * from money order by record_date desc";
        Cursor cursor = sqLiteDatabase.rawQuery(sql, null);

        if (cursor.getCount() > 0) {
            cursor.moveToFirst();
            for (int i = 0; i < cursor.getCount(); i++) {
                int id = cursor.getInt(cursor.getColumnIndex("id"));
                double cloth = cursor.getDouble(cursor.getColumnIndex("cloth"));
                double food = cursor.getDouble(cursor.getColumnIndex("food"));
                double house = cursor.getDouble(cursor.getColumnIndex("house"));
                double use = cursor.getDouble(cursor.getColumnIndex("use"));
                double walk = cursor.getDouble(cursor.getColumnIndex("walk"));
                double other = cursor.getDouble(cursor.getColumnIndex("other"));
                double get = cursor.getDouble(cursor.getColumnIndex("get_money"));
                String record_date = cursor.getString(cursor.getColumnIndex("record_date"));
                list.add(new Money(id, cloth, food, house, use, walk, other, get, record_date));
                cursor.moveToNext();
            }
            moneyAdapter = new MoneyAdapter(getActivity(), list);
            moneyLV.setAdapter(moneyAdapter);
            money_warnTV.setVisibility(View.GONE);
            moneyLV.setVisibility(View.VISIBLE);
        } else {
            money_warnTV.setVisibility(View.VISIBLE);
            moneyLV.setVisibility(View.GONE);
        }

        moneyLV.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> adapterView, View view, int i, long l) {
                Dialog myDialog = new Dialog(getActivity());
                myDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
                Window window = myDialog.getWindow();
                window.setContentView(R.layout.del_course_dialog);
                myDialog.show();
                Button course_okBtn = window.findViewById(R.id.course_del_okBtn);
                Button course_cancelBtn = window.findViewById(R.id.course_del_cancelBtn);
                course_cancelBtn.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        myDialog.dismiss();
                    }
                });
                course_okBtn.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        try {
                            // 删除操作
                            String del_sql = "delete from money where id=" + list.get(i).getId();
                            sqLiteDatabase.execSQL(del_sql);
                            list.remove(i);
                            moneyAdapter.notifyDataSetChanged();
                            myDialog.dismiss();
                            Toast.makeText(getActivity(), getString(R.string.money_del_ok), Toast.LENGTH_SHORT).show();

                        } catch (Exception e) {
                            Toast.makeText(getActivity(), getString(R.string.money_del_failed), Toast.LENGTH_SHORT).show();
                        }
                    }
                });
                return true;
            }
        });

        money_addBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getActivity(), AddMoneyActivity.class);
                startActivity(intent);
            }
        });
        money_searchIV.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent =new Intent(getActivity(), SearchRecordActivity.class);
                startActivity(intent);
            }
        });
        money_warnTV.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent =new Intent(getActivity(), DetailRecordActivity.class);
                startActivity(intent);
            }
        });
        money_aiIV.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent =new Intent(getActivity(), AIRecordActivity.class);
                startActivity(intent);
            }
        });


        return root;

    }
}